package org.i_plus.model.services;

import java.util.List;

import org.i_plus.model.GiftBean;
import org.i_plus.model.dao.GiftDAOHibernate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class GiftService {

	private GiftDAOHibernate dao = null;
	public void setGiftDAOHibernate(GiftDAOHibernate dao){
		this.dao = dao;
	}
	
	public static void main(String args[]){
		ApplicationContext context = new ClassPathXmlApplicationContext("beans.config.xml");
		GiftService service = (GiftService)context.getBean("giftService");
		List<GiftBean> list = service.find();
		for(GiftBean temp:list){
			System.out.println(temp.getGiftid()+":"+temp.getName());
		}
		((ConfigurableApplicationContext)context).close();
	}
	
	
	public GiftBean insert(int giftid,String name,byte[] pic,int pop,int gmoney,int vip){

		dao.insertG(giftid, name, pic, pop, gmoney, vip);
		return dao.selectById(giftid);

	}
	public GiftBean update(int giftid,String name,byte[] pic,int pop,int gmoney,int vip){
		GiftBean temp = null;
		if(pic==null){//如果前端輸入null 則用舊圖回填
			temp = dao.selectById(giftid);
			pic = temp.getPic();
		}
		dao.updateG(giftid, name, pic, pop, gmoney, vip);
		return dao.selectById(giftid);
	}
	public boolean delete(int giftid){
		try {
			dao.deleteG(giftid);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	public GiftBean findByPK(int giftid){
		return dao.selectById(giftid);
	}
	public List<GiftBean> find(){
		return dao.selectAll();
	}
	public int[] giftCount(){
    	int[] result = dao.selectCount();
    	return result;
	}
	
	
	
	
}
